<?php

namespace common\models\ofc;

use Yii;
use common\helpers\ArrayHelper;
use youmbe\sports\NanoSports;
use common\models\mongo\Season;

/**
 * This is the model class for table "{{%ofc_season}}".
 *
 * @property int $id
 * @property int $competition_id 赛事ID
 * @property string $name 赛季名称
 * @property string $logo 赛季LOGO
 * @property int $status 状态
 * @property int $current 是否当前赛季
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $channel_id 第三方信息ID
 */
class UpdateSeasonLogic extends \common\db\ActiveRecord {

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%ofc_season}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['competition_id', 'status', 'current', 'created_at', 'updated_at', 'channel_id'], 'integer'],
            [['name'], 'required'],
            [['name'], 'string', 'max' => 50],
            [['logo'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'competition_id' => '赛事ID',
            'name' => '赛季名称',
            'logo' => '赛季LOGO',
            'status' => '状态',
            'current' => '是否当前赛季',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'channel_id' => '第三方信息ID',
        ];
    }

    public function getCompetition() {
        return $this->hasOne(OfcCompetition::className(), ['id' => 'competition_id']);
    }

    public function afterSave($insert, $changedAttributes) {
        $channelId = ArrayHelper::getValue($changedAttributes, 'channel_id');
        if ($channelId && $channelId !== $this->channel_id) {
            $this->on(self::EVENT_AFTER_UPDATE, [$this, 'updateSeason'], $this);
        }
        Yii::error([
            'mode' => $insert ? self::EVENT_AFTER_INSERT : self::EVENT_AFTER_UPDATE,
            'changedAttributes' => $changedAttributes,
            'channel_id' => $this->channel_id,
            'status' => $channelId . ' !== ' . $this->channel_id,
        ]);
        parent::afterSave($insert, $changedAttributes);
    }

    public function updateSeason($event) {
        $model = $event->data;
        $seasonid = $model->channel_id;
        if (!$seasonid) {
            Yii::error([
                'id' => $model->id,
                'channel_id' => $model->channel_id,
            ], __METHOD__);
            return;
        }
        $sports = new NanoSports();
        $data = $sports->season($seasonid);
        Yii::error([
            'id' => $model->id,
            'channel_id' => $model->channel_id,
            'data' => $data,
        ], __METHOD__);
        $mseason = Season::findOrNew([
            'id' => $model->id,
            'channel_id' => $model->channel_id,
        ]);
        if ($mseason) {
            $mseason->attributes = $data;
        }
        $mseason->save();
    }

    public function updateStage($event) {
        echo 'recond';
//        Yii::error($event->data);
        $taskid = Yii::$app->queue->push(new \common\jobs\AfterSeasonJob([
            'season' => $this,
            'data' => $event->data,
        ]));
        Yii::error('创建任务队列:' . $taskid, __METHOD__);
        $taskid = Yii::$app->queue->push(new \common\jobs\TestJob([
            'time' => date('Y-m-d H:i:s'),
            'data' => ['aaa'],
        ]));
    }

}
